import json
import requests
import time
import hmac
import hashlib
import base64
import urllib.parse


class Notcie:
    def get_signature(self,content):
        timestamp = str(round(time.time() * 1000))
        secret = 'SEC3e3bb38c20bcc1748e57c34b64cd7218eac43e19795382d682abf239138769e5'
        access_token='9b1d22a057eef3ed4e7bbac54ef977d891d6413213d339151640cf4a839a0f6a'
        secret_enc = secret.encode('utf-8')
        string_to_sign = '{}\n{}'.format(timestamp, secret)
        string_to_sign_enc = string_to_sign.encode('utf-8')
        hmac_code = hmac.new(secret_enc, string_to_sign_enc, digestmod=hashlib.sha256).digest()
        sign = urllib.parse.quote_plus(base64.b64encode(hmac_code))
        url = f'https://oapi.dingtalk.com/robot/send?access_token={access_token}&timestamp={timestamp}&sign={sign}'
        data = {"msgtype": "text", "text": {"content": content}}
        res = requests.post(url, data=json.dumps(data), headers={'Content-Type': 'application/json'})
        if res.status_code == 200:
            return f"{data}信息发送成功"
